-
Notifications
You must be signed in to change notification settings - Fork 48
feat(ai): Add $ai_framework property for framework integrations
#347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Adds framework identification metadata to all AI events for easier filtering and analytics. Each integration now includes a $ai_lib_metadata property with schema version and framework name. - LangChain: Hardcoded to "langchain" - Native wrappers (Anthropic, OpenAI, Gemini): Uses provider name - Ready for future frameworks (pydantic-ai, crewai, llamaindex) This enables PostHog queries to easily distinguish between: - Direct SDK wrapper usage - Framework-mediated usage (LangChain, etc.) - Different framework types 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
- Added \$ai_lib_metadata to call_llm_and_track_usage (sync) - Added \$ai_lib_metadata to call_llm_and_track_usage_async (async) - Added test assertion in test_basic_completion - Placed metadata at end of properties for consistency All tests pass successfully. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
$ai_lib_metadata to AI integrations
Creates a single `get_ai_lib_metadata(framework)` utility function to generate the $ai_lib_metadata object, replacing inline implementations across the codebase. Changes: - Add get_ai_lib_metadata() utility to utils.py - Update LangChain callbacks to use utility function - Update call_llm_and_track_usage() to use utility function - Update call_llm_and_track_usage_async() to use utility function - Update capture_streaming_event() to use utility function Benefits: - Consistency across all integrations - Single source of truth for metadata structure - Easier to extend with version detection later 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 files reviewed, 1 comment
|
|
||
| assert call_args["distinct_id"] == "test-id" | ||
| assert call_args["event"] == "$ai_generation" | ||
| assert props["$ai_lib_metadata"] == { | ||
| "schema": "v1", | ||
| "frameworks": [{"name": "openai"}], | ||
| } | ||
| assert props["$ai_provider"] == "openai" | ||
| assert props["$ai_model"] == "gpt-4" | ||
| assert props["$ai_input"] == [{"role": "user", "content": "Hello"}] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Check that Anthropic, Gemini, and LangChain tests also assert $ai_lib_metadata
This test correctly validates the new metadata field, but similar assertions are missing in other provider tests:
posthog/test/ai/anthropic/test_anthropic.py:276(test_basic_completion)posthog/test/ai/gemini/test_gemini.py:147(test_new_client_basic_generation)posthog/test/ai/langchain/test_callbacks.py:160(test_basic_chat_chain)
Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog/test/ai/openai/test_openai.py
Line: 482:491
Comment:
**style:** Check that Anthropic, Gemini, and LangChain tests also assert `$ai_lib_metadata`
This test correctly validates the new metadata field, but similar assertions are missing in other provider tests:
- `posthog/test/ai/anthropic/test_anthropic.py:276` (`test_basic_completion`)
- `posthog/test/ai/gemini/test_gemini.py:147` (`test_new_client_basic_generation`)
- `posthog/test/ai/langchain/test_callbacks.py:160` (`test_basic_chat_chain`)
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add missing $ai_lib_metadata assertions to Anthropic, Gemini, and LangChain tests to match the validation already present in OpenAI tests. Each test now verifies the metadata field contains the correct schema version and framework name. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea! 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure to update version.py and the changelog!
Changes:
- Replace complex $ai_lib_metadata object with simple $ai_framework string
- Only include $ai_framework when using actual framework (LangChain)
- Remove $ai_framework from direct provider calls (OpenAI, Anthropic, Gemini)
- Update all tests to reflect new behavior
Before: {"schema": "v1", "frameworks": [{"name": "langchain"}]}
After: "langchain" (only when using LangChain framework)
This eliminates wasteful redundancy where framework=provider for direct calls.
$ai_lib_metadata to AI integrations$ai_framework property for framework integrations
Update version and changelog for PR #347
Update version and changelog for PR #347
Summary
Adds a simple
$ai_frameworkproperty to AI events, only when an actual framework layer is used (e.g., LangChain). This helps identify which abstraction layer is being used on top of the underlying AI provider.Changes
$ai_frameworkproperty to AI events for framework integrations (LangChain)"langchain")Example Events
Framework call (LangChain):
{ "$ai_framework": "langchain", "$ai_provider": "openai", "$ai_model": "gpt-4", ... }Direct provider call (OpenAI):
{ "$ai_provider": "openai", "$ai_model": "gpt-4", ... }Rationale
This approach eliminates redundancy - we only add
$ai_frameworkwhen there's an actual framework layer. For direct provider calls, the framework would just duplicate the provider name, which is wasteful.Testing
$ai_frameworkin LangChain tests